<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="安全检查员id" prop="inspectorId">
      <el-input v-model="dataForm.inspectorId" placeholder="安全检查员id"></el-input>
    </el-form-item>
    <el-form-item label="安全检查时间" prop="inspectionDate">
      <el-input v-model="dataForm.inspectionDate" placeholder="安全检查时间"></el-input>
    </el-form-item>
    <el-form-item label="检查的楼栋id" prop="buildingId">
      <el-input v-model="dataForm.buildingId" placeholder="检查的楼栋id"></el-input>
    </el-form-item>
    <el-form-item label="检查的楼层id" prop="floorId">
      <el-input v-model="dataForm.floorId" placeholder="检查的楼层id"></el-input>
    </el-form-item>
    <el-form-item label="检查的房屋id" prop="houseId">
      <el-input v-model="dataForm.houseId" placeholder="检查的房屋id"></el-input>
    </el-form-item>
    <el-form-item label="检查的消防设备id" prop="deviceId">
      <el-input v-model="dataForm.deviceId" placeholder="检查的消防设备id"></el-input>
    </el-form-item>
    <el-form-item label="检查的结果" prop="result">
      <el-input v-model="dataForm.result" placeholder="检查的结果"></el-input>
    </el-form-item>
    <el-form-item label="安全检查的照片地址" prop="photos">
      <el-input v-model="dataForm.photos" placeholder="安全检查的照片地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          inspectorId: '',
          inspectionDate: '',
          buildingId: '',
          floorId: '',
          houseId: '',
          deviceId: '',
          result: '',
          photos: ''
        },
        dataRule: {
          inspectorId: [
            { required: true, message: '安全检查员id不能为空', trigger: 'blur' }
          ],
          inspectionDate: [
            { required: true, message: '安全检查时间不能为空', trigger: 'blur' }
          ],
          buildingId: [
            { required: true, message: '检查的楼栋id不能为空', trigger: 'blur' }
          ],
          floorId: [
            { required: true, message: '检查的楼层id不能为空', trigger: 'blur' }
          ],
          houseId: [
            { required: true, message: '检查的房屋id不能为空', trigger: 'blur' }
          ],
          deviceId: [
            { required: true, message: '检查的消防设备id不能为空', trigger: 'blur' }
          ],
          result: [
            { required: true, message: '检查的结果不能为空', trigger: 'blur' }
          ],
          photos: [
            { required: true, message: '安全检查的照片地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/inspection/inspection/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.inspectorId = data.inspection.inspectorId
                this.dataForm.inspectionDate = data.inspection.inspectionDate
                this.dataForm.buildingId = data.inspection.buildingId
                this.dataForm.floorId = data.inspection.floorId
                this.dataForm.houseId = data.inspection.houseId
                this.dataForm.deviceId = data.inspection.deviceId
                this.dataForm.result = data.inspection.result
                this.dataForm.photos = data.inspection.photos
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/inspection/inspection/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'inspectorId': this.dataForm.inspectorId,
                'inspectionDate': this.dataForm.inspectionDate,
                'buildingId': this.dataForm.buildingId,
                'floorId': this.dataForm.floorId,
                'houseId': this.dataForm.houseId,
                'deviceId': this.dataForm.deviceId,
                'result': this.dataForm.result,
                'photos': this.dataForm.photos
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
